<?php

namespace App\Http\Service;

use App\Libs\HttpCurlLibrary;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use App\Models\Forbiddenwords;
use App\Models\Forbiddenwordslog;
use App\Http\Service\BaseService;
use App\Http\Service\ForbiddenwordslogService;
use App\Http\Service\JiebaService;

//关键词记录
class ForbiddenwordsService extends BaseService
{
   
    public $forbiddenwords;
    public $imagecheck;
    public $contentcheck;
    public $model;
    public function __construct(Forbiddenwords $model,Forbiddenwordslog $forbiddenwordslog,JiebaService $JiebaService,ForbiddenwordslogService $forbiddenwordslogService)
    {
        parent::__construct();
        $this->imagecheck=config('myurl.imagecheck.url');
        $this->contentcheck=config('myurl.contentcheck.url');
        $this->model=$model;
        $this->forbiddenwordslog=$forbiddenwordslog;
        $this->JiebaService=$JiebaService;
        $this->forbiddenwordslogService=$forbiddenwordslogService;
    }
    #风险等级
    public function checkRiskLeve($Leve)
    {
        $RiskLeves=[
            'high'=>[
                'label'=>'high',
                'meaning'=>'高风险',
                'level'=>'5'
            ],
            'medium'=>[
                'label'=>'medium',
                'meaning'=>'中风险',
                'level'=>'4'
            ],
            'low'=>[
                'label'=>'low',
                'meaning'=>'低风险',
                'level'=>'3'
            ],
            'none'=>[
                'label'=>'none',
                'meaning'=>'未检测到风险',
                'level'=>'0'
            ],
        ];
        if(isset($RiskLeves[$Leve])){
            return $RiskLeves[$Leve];
        }else{
            $result=[
                'label'=>'none',
                'meaning'=>'未检测到风险',
                'level'=>'0'
            ];
            return $result;
        }
        return $labels;
    }

    #风险标签
    public function checkContentLable($label)
    {
        $labels = [ 
            'inappropriate_ethics'=>[ 
                'label' => 'inappropriate_ethics', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似不良价值观内容' 
            ],

            'inappropriate_profantly'=>[ 
                'label' => 'inappropriate_profantly', 
                'confidence' => '0~100 分，分数越高置信度越高', 
                'meaning' => '疑似攻击辱骂内容' 
            ],

            'inappropriate_oral'=>[ 
                'label' => 'inappropriate_oral',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似低俗口头语内容' 
            ], 

            'inappropriate_superstition'=>[ 
                'label' => 'inappropriate_superstition',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似封建迷信内容' 
            ],

            'inappropriate_nonsense'=>[ 
                'label' => 'inappropriate_nonsense',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似无意义灌水内容' 
            ],

            'pt_to_sites'=>[ 
                'label' => 'pt_to_sites',
                'confidence' => '0~100 分，分数越高置信度越高', 
                'meaning' => '疑似站外引流' 
            ], 

            'pt_by_recruitment'=>[ 
                'label' => 'pt_by_recruitment',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似网赚兼职广告' 
            ], 

            'pt_to_contact'=>[ 
                'label' => 'pt_to_contact',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似引流广告号' 
            ], 
            'religion_b'=>[ 
                'label' => 'religion_b',
                'confidence' => '0~100 分，分数越高置信度越高', 
                'meaning' => '疑似涉及佛教' 
            ], 
            'religion_t'=>[ 
                'label' => 'religion_t',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及道教'
            ], 
            'religion_c'=>[ 
                'label' => 'religion_c',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及基督教' 
            ], 
            'religion_j'=>[ 
                'label' => 'religion_j', 
                'confidence' => '0~100 分，分数越高置信度越高', 
                'meaning' => '疑似涉及伊斯兰教'
            ],
            'religion_h'=>[ 
                'label' => 'religion_h',
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及印度教' 
            ],
            'customized'=>[ 
                'label' => 'customized', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '命中自定义词库' 
            ],
            'pornographic_adult'=>[ 
                'label' => 'pornographic_adult', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似色情内容' 
            ],
            'sexual_terms'=>[ 
                'label' => 'sexual_terms', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似性健康内容' 
            ],
            'sexual_suggestive'=>[ 
                'label' => 'sexual_suggestive', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似低俗内容' 
            ],
            'political_figure'=>[ 
                'label' => 'political_figure', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似政治人物' 
            ],
            'political_entity'=>[ 
                'label' => 'political_entity', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似政治实体' 
            ],
            'political_n'=>[ 
                'label' => 'political_n', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似敏感政治内容' 
            ],
            'political_p'=>[ 
                'label' => 'political_p', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉政禁宣人物' 
            ],
            'political_a'=>[ 
                'label' => 'political_a', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '涉政专项升级保障' 
            ],
            'violent_extremist'=>[ 
                'label' => 'violent_extremist', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似极端组织' 
            ],
            'violent_incidents'=>[ 
                'label' => 'violent_incidents', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似极端主义内容' 
            ],
            'violent_weapons'=>[ 
                'label' => 'violent_weapons', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似武器弹药' 
            ],
            'contraband_drug'=>[ 
                'label' => 'contraband_drug', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似毒品相关' 
            ],
            'contraband_gambling'=>[ 
                'label' => 'contraband_gambling', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似赌博相关' 
            ],
            'contraband_act'=>[ 
                'label' => 'contraband_act', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似违禁行为' 
            ],
            'contraband_entity'=>[ 
                'label' => 'contraband_entity', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似违禁工具' 
            ],
            'inappropriate_discrimination'=>[ 
                'label' => 'inappropriate_discrimination', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似偏见歧视内容' 
            ],
            'inappropriate_ethics'=>[ 
                'label' => 'inappropriate_ethics', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似不良价值观内容' 
            ],
            'inappropriate_profanity'=>[ 
                'label' => 'inappropriate_profanity', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似攻击辱骂内容' 
            ],
            'inappropriate_oral'=>[ 
                'label' => 'inappropriate_oral', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似低俗口头语内容' 
            ],
            'inappropriate_superstition'=>[ 
                'label' => 'inappropriate_superstition', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似封建迷信内容' 
            ],
            'inappropriate_nonsense'=>[ 
                'label' => 'inappropriate_nonsense', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似无意义灌水内容' 
            ],
            'pt_to_sites'=>[ 
                'label' => 'pt_to_sites', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似站外引流' 
            ],
            'pt_by_recruitment'=>[ 
                'label' => 'pt_by_recruitment', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似网赚兼职广告' 
            ],
            'pt_to_contact'=>[ 
                'label' => 'pt_to_contact', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似引流广告号' 
            ],
            'religion_b'=>[ 
                'label' => 'religion_b', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及佛教' 
            ],
            'religion_t'=>[ 
                'label' => 'religion_t', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及道教' 
            ],
            'religion_c'=>[ 
                'label' => 'religion_c', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及基督教' 
            ],
            'religion_i'=>[ 
                'label' => 'religion_i', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及伊斯兰教' 
            ],
            'religion_h'=>[ 
                'label' => 'religion_h', 
                'confidence' => '0~100 分，分数越高置信度越高',
                'meaning' => '疑似涉及印度教' 
            ],

                    
        ];
        if(isset($labels[$label])){
            return $labels[$label];
        }else{
            $result=[ 
                'label' => 'nonLabel', 
                'confidence' => '0~100 分，分数越高置信度越高', 
                'meaning' => '未检测出风险'
            ];
            return $result;
        }
        return $labels;
    }
    #检测内容
    public function checkContent($content,$people_id="")
    {
        //dd($part_word); 
        $url=$this->contentcheck;
        $postdata=[
            'content'=>$content
        ];

        $code=json_post($url, $postdata);
        if($code['status']==200){
            #分数
            $confidence=$code['confidence'];
            $label=$code['label'];
            $RiskLevel=0;
            if(isset($code['details'])){
                $details=$code['details'];
                if(isset($details['api_response'])){
                    $api_response=$details['api_response'];
                    if(isset($api_response['Data'])){
                        $Data=$api_response['Data'];
                        if(isset($Data['RiskLevel'])){
                            $RiskLevel=$Data['RiskLevel'];
                        }

                    }
                }
            }

            
            if($confidence>=0.5){
                $msg_ar=$this->checkContentLable($label);
                $msg=$msg_ar['meaning'];

                $Leve_ar=$this->checkRiskLeve($RiskLevel);
                #表添加关键词
                $worddata=[
                    'word'=>$content,
                    'category'=>$msg_ar['label'],
                    'category_desc'=>$msg_ar['meaning'],
                    'level'=>$Leve_ar['level'],
                    'replacement'=>'*******',
                    'status'=>'1',
                ];
                //dd($worddata);
                $this->addData($worddata);
                #添加日志
                $wordlogdata=[
                    'found_words'=>$content,
                    'action_taken'=>'blocked',
                    'user_id'=>$people_id,

                ];
                $this->forbiddenwordslogService->addData($wordlogdata);
                $this->error("您发送【{$msg}】,请规范发言");
            }
            


        }
    }
    #添加
    public function addData($data)
    {
        #分词
        #$part_word=$this->JiebaService->part_word($content);  

        $where=['word'=>$data['word']];
        $first=$this->model->where($where)->first();
        if($first){
            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->update($data);

        }else{
            $data['create_time']=date("Y-m-d h:i:s");
            $data['update_time']=date("Y-m-d h:i:s");
            $this->model->where($where)->insertGetId($data);
        }


    }

   
    

}
